import router from "./router/index";
import store from "./src/store";
let refresh = true;

export default function () {
  // 导航守卫
  router.beforeEach(async (to, from, next) => {
    document.title = to.meta?.title;
    // let isLogin = localStorage.getItem("admin_login");
    // 通过token判断是否登录    跳转成功
    let isLogin = store.state.user.user.token;
    // console.log(isLogin)
    if (isLogin) {
      if (refresh) {
        await store.dispatch("user/GET_ROUTES");
        refresh = false;
        // next 动态添加路由之后需要next(指定跳转路由)
        next(to.path);
      } else {
        next();
      }
    } else {
      if (to.name == "login") {
        next();
      } else {
        next({ path: "login", query: { redirect: to.path } });
      }
    }
  });
}
